import numpy as np


# 线性拉伸
def imadjust(in_img, low, high):
    out_img = in_img * ((low <= in_img) & (in_img <= high)) + high * (in_img > high) + low * (in_img < low)
    out_img = (out_img - low) / (high - low) * 255
    out_img = np.uint8(out_img)
    return out_img


# 根据颜色表将灰度转RGB显示
def ima_to_rgb(in_img, RGBlist):
    value_max = np.max(in_img)
    value_min = np.min(in_img)
    diff = value_max - value_min
    in_img1 = imadjust(in_img, value_min, value_max)
    h = in_img.shape[0]
    w = in_img.shape[1]
    T = in_img1.reshape(1, h * w)

    R = RGBlist[T, 0]
    G = RGBlist[T, 1]
    B = RGBlist[T, 2]
    R = R.reshape(h, w)
    G = G.reshape(h, w)
    B = B.reshape(h, w)

    out_img = np.zeros((h, w, 3), 'uint8')
    out_img[:, :, 0] = R
    out_img[:, :, 1] = G
    out_img[:, :, 2] = B

    return out_img
